// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    searchList: [],
    show: false,
    searchOldVal: '',
    searchEndList: [],
    hotSearchList: []
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  jumpGo(e) {
    console.log(e.currentTarget.dataset.msg)
    console.log(e.currentTarget.dataset.name)
    let msg = e.currentTarget.dataset.msg
    let poetName = e.currentTarget.dataset.name
    wx.setStorageSync('msg', msg)
    wx.setStorageSync('poetName', poetName)
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  jumpGo2(e) {
    console.log(e.currentTarget.dataset.msg)
    this.setData({
      searchOldVal: e.currentTarget.dataset.msg
    })
    this.getSearchList2()
  },
  empty() {
    wx.showModal({
      title: '提示',
      content: '确定删除搜索记录吗',
      confirmColor: "#FDB201",
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {
          wx.removeStorage({
            key: "searchList"
          })
          this.setData({
            searchList: []
          })
        }
      }
    })

  },

  getSearchList() {
    // http://localhost:3000/poetry/searchList
    let that = this;

    return new Promise((reslove, reject) => {
      this.setData({
        searchOldVal: this.data.searchValue,
      })
      wx.request({
        url: 'http://localhost:3000/poetry/searchList',
        method: "POST",
        data: {
          name: that.data.searchOldVal
        },
        success(data) {
          console.log(data.data.rows)
          data.data.rows.map((item) => {
            item.poet = JSON.parse(item.poet)
            item.tags = JSON.parse(item.tags)
          })
          that.setData({
            searchEndList: data.data.rows
          })
          wx.hideLoading()
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  getSearchList2() {
    // http://localhost:3000/poetry/searchList
    let that = this;
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/searchList',
        method: "POST",
        data: {
          name: that.data.searchOldVal
        },
        success(data) {
          console.log(data.data.rows)
          data.data.rows.map((item) => {
            item.poet = JSON.parse(item.poet)
            item.tags = JSON.parse(item.tags)
          })

          that.setData({
            searchEndList: data.data.rows,
            show: true
          })
          wx.hideLoading()
          resolve({
            code: 200,
            msg: "搜索成功"
          })
        }
      })
    })

  },
  getHotList() {
    let that = this
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/sqlData/hotList2',
        method: "POST",
        success(data) {
          console.log(data.data)
          data.data.rows.map((item) => {
            item.poet = JSON.parse(item.poet)
            item.tags = JSON.parse(item.tags)
          })
          that.setData({
            hotSearchList: data.data.rows
          })
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  selval(e) {
    console.log(e.detail.value)
    this.setData({
      searchValue: e.detail.value
    })
  },
  async jumpGo3(e) {
    // console.log(e.currentTarget.dataset.msg)
    // console.log(e.currentTarget.dataset.name)
    let msg = e.currentTarget.dataset.msg

    let poetName = e.currentTarget.dataset.name
    wx.setStorageSync('msg', msg)
    wx.setStorageSync('poetName', poetName)
    let searchList = wx.getStorageSync('searchList')
    if (!searchList) {
      searchList = [];
      searchList.push(msg)
      await this.getSearchList()
      wx.setStorageSync('searchList', JSON.stringify(searchList))
      console.log(22)
    } else {
      searchList = JSON.parse(searchList)
      let slt = searchList.find((item) => {
        return item == msg
      })
      if (slt == undefined) {
        searchList.push(msg)
        wx.setStorageSync('searchList', JSON.stringify(searchList))
      }

    }
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  async search() {

    let searchList = wx.getStorageSync('searchList')
    if (this.data.searchValue != "") {
      wx.showLoading({
        title: '搜索中',
      })
      if (!searchList) {
        searchList = [];
        searchList.push(this.data.searchValue)
        await this.getSearchList()

        wx.setStorageSync('searchList', JSON.stringify(searchList))
        console.log(22)
      } else {
        await this.getSearchList()
        searchList = JSON.parse(searchList)
        let slt = searchList.find((item) => {
          return item == this.data.searchValue
        })
        if (slt == undefined) {
          searchList.push(this.data.searchValue)
          wx.setStorageSync('searchList', JSON.stringify(searchList))
        }

      }
      this.setData({
        searchList: searchList,
        show: true,
        searchValue: "",
      })

    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync('searchList')) {
      let searchList = JSON.parse(wx.getStorageSync('searchList'));
      this.setData({
        searchList: searchList
      })
    }
    this.getHotList()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})